﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLZJFurnitureSCM.Model;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Data;

namespace QLZJFurnitureSCM.DAL
{
    public class OrderDAO:IBaseDAO<Order>, IOrderDAO
    {
        private string connectionString = DALFactory.CONNECTIONSTRING;

        private SqlConnection sqlConn = null;

        public OrderDAO()
        {
            sqlConn = new SqlConnection(connectionString);
        }

        public Order Create(Order t)
        {
            try
            {
                string query = string.Format("INSERT INTO [Orders]([orderByUser],[orderContact],[orderPayStatus],[orderDriveEmployee],[orderNo],[orderTime],[orderPay]) values('{0}','{1}',{2},'{3}','{4}','{5}',{6}); select @@identity;", 
                    t.OrderByUser, t.OrderContact, t.OrderPayStatus ? 1 : 0, t.OrderDriveEmployee, t.OrderNo, t.OrderTime, t.OrderPay);
                if (sqlConn.State != ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand sqlCmd = new SqlCommand(query, sqlConn);
                Console.WriteLine(query);
                object o = sqlCmd.ExecuteScalar();
                sqlConn.Close();
                if (o != null)
                    t.OrderId = int.Parse(o.ToString());
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return Create(t);
            }
            return t;
        }

        public Order Read(Order t)
        {
            throw new NotImplementedException();
        }

        public bool Update(Order t)
        {
            throw new NotImplementedException();
        }

        public bool UpdatePayStatus(Order order)
        {
            DataContext dataContext = new DataContext(connectionString);
            try
            {
                string query = string.Format("update Orders set orderPayStatus = 1 where orderId = {0}; ", order.OrderId);
                Console.WriteLine(query);
                dataContext.ExecuteQuery<Order>(query);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
            return true;
        }

        public bool Delete(Order t)
        {
            throw new NotImplementedException();
        }

        public List<Order> ReadAll()
        {
            DataContext dataContext = new DataContext(connectionString);
            IEnumerable<Order> enumOrder = dataContext.ExecuteQuery<Order>("select * from orders;");
            return enumOrder.ToList();
        }

        public List<Order> FindUnPayOrder()
        {
            DataContext dataContext = new DataContext(connectionString);
            IEnumerable<Order> enumOrder = dataContext.ExecuteQuery<Order>("select * from orders where orderPayStatus = 0;");
            return enumOrder.ToList();
        }
    }
}
